package com.mediastory.spsserver.guide.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mediastory.spsserver.guideFile.service.GuideFileSvc;
import com.mediastory.spsserver.guide.domain.Guide;
import com.mediastory.spsserver.guide.service.GuideSvc;

/**
 * @author Jaewon Choi
 */

@Controller
@RequestMapping(value={"/guide"})
public class GuideCtrl {
	
	private final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private GuideSvc guideSvc;
	
	@Autowired
	private GuideFileSvc guideFileSvc;
	
	
	/**
	 * 등록,수정 폼
	 * */
	@RequestMapping(value={"/form.do"})
	public Map<String, Object> form(Guide guide, HttpServletRequest request, HttpSession session) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		//카테고리 검색 조건
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("gubun", "guide");
		
		//customerIdx
		resultMap.put("guide", guideSvc.findby(condition));

		if(resultMap != null) {
			//이미지 파일 목록

			condition.put("fileType", "guideImg");
			condition.put("contIdx", guideSvc.findbyIdx(condition));
			resultMap.put("guideImgs", guideFileSvc.findbyList(condition));
		}
		
		
		return resultMap;
	}
	
	/**
	 * 콘텐츠 등록/수정
	 * */
	@RequestMapping(value={"/submit.do"}, method=RequestMethod.POST)
	public String submit(Guide guide, HttpServletRequest request, HttpSession session) throws Exception {
		//customerIdx
		Integer cIdx = (Integer)session.getAttribute("cIdx");
		String userId = (String)session.getAttribute("userId");
		String ip = request.getRemoteAddr();
		guide.setcIdx(cIdx);
		
		if(guide.getIdx() != null){
			guide.setModifiedId(userId);
			guide.setModifiedIp(ip);
			guideSvc.modify(guide);
		}else{
			guide.setCreatedId(userId);
			guide.setCreatedIp(ip);
			guideSvc.add(guide);
		}

		return "guide/result";
	}
	
}